﻿; #Include %A_ScriptDir%\Lib\TrayIcon.ahk
; #Include %A_ScriptDir%\Lib\WinClipAPI.ahk
; #Include %A_ScriptDir%\Lib\WinClip.ahk
/*
Plugin=DM_SendFilesByTIM
Name1=通过TIM发送文件
Command1=DM_SendFilesByTIM
Author=Kawvin
Moder Author=BoBO
Version=0.2版本
说明：批量添加，由|分开,=为分隔符,如：张三|李四||王五
1.匹配Tim最新版3.0.0版本
*/
DM_SendFilesByTIM(aParam){    ; 此Plugin执行的command为Test，代表执行Test函数,函数请预留一个aParam，用于QuickZ传递参数过来。
	icon:=A_ScriptDir . "\User\Icons\TIM.ico"
    ;Menu_SendFilesByTIM := MenuZ_GetSibling() ; 获取一个菜单对象，不是子菜单，是同级菜单
	Menu_SendFilesByTIM := MenuZ_GetSub() ; 获取一个子菜单对象
	Menu_SendFilesByTIM.SetParams({iconsSize:20})
	IndexArray:=["1","2","3","4","5","6","7","8","9","0","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
	Menu_SendFilesByTIM.Add({name:"&1  发送到  我的Android手机",icon: icon,uid:{Handle:"DM_SendFilesByTIM_Handle",Data:"My_Android"}}) ; 安卓手机，苹果手机注释本行
	Menu_SendFilesByTIM.Add({name:"&2  发送到  我的iPhone",icon: icon,uid:{Handle:"DM_SendFilesByTIM_Handle",Data:"My_Apple"}})  ; 苹果手机，安卓手机注释本行
	Menu_SendFilesByTIM.Add()
	if !strlen(aParam)
		Menu_SendFilesByTIM.Add({name:"批量添加，由|分开,||为分隔符,如：张三|李四||王五,",icon: icon,uid:{Handle:"DM_SendFilesByTIM_Handle",Data:""}}) 
	Index:=3
	loop,parse, aParam,`|
	{
		MenuString:=trim(A_LoopField)
		if (MenuString!="") 
		{
			Menu_SendFilesByTIM.Add({name:"&" Index "  发送到  " MenuString,icon: icon,uid:{Handle:"DM_SendFilesByTIM_Handle",Data:MenuString}}) ; Handle的值为点击菜单后执行的功能
			Index+=1
		}
		else
			Menu_SendFilesByTIM.Add()
	}
	Menu_SendFilesByTIM.Add()
	Menu_SendFilesByTIM.Add({name:"&Z  搜索联系人",icon: icon,uid:{Handle:"DM_SendFilesByTIM_Handle",Data:"搜索联系人"}}) 
    return Menu_SendFilesByTIM  ; 必须返回子菜单对象
}

DM_SendFilesByTIM_Handle(aMsg, aObj)
{
    If (aMsg = "OnRun")
		Menu_SendFilesByTIM_Action(aObj.Uid.Data)
	;~ else If (aMsg = "OnRButton")			
	;~ else If (aMsg = "onmbutton")
}

Menu_SendFilesByTIM_Action(TIMName)
{
	if (!strlen(TIMName))
		return
	MySel:=QZData("files")
	;准备搜索数据
	if(TIMName="My_Android")
		TemTIMName:="Android"
	else if(TIMName="My_Apple")
		TemTIMName:="iPhone"
	else if(TIMName="搜索联系人")
	{
		InputBox,TemTIMName,搜索好友、聊天记录,,,200,100
		if ErrorLevel
			return
		if (TemTIMName="")
			return
	} else {
		TemTIMName:=TIMName
	}
	;获取TIM进程数量
	TIMCount:=0
	for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
	{
		if (process.Name="TIM.exe")
			TIMCount+=1
	}
	;等待激活TIM
	if (TIMCount>1)
	{
		TrayTip, 提醒,  选择需传送文件的TIM号!
		while 1
		{
			WinGetTitle, Cur_Title, A
			if (Cur_Title = "TIM")
				break
		}
	} else {
		BlockInput, on
		TrayIcon_Button("TIM.exe","L")
		; sleep,100
		;~ TrayIcon_Button("TIM.exe","R")
		;~ sleep,200
		;~ send,{up}
		;~ send,{up}
		;~ send,{up}
		;~ send,{enter}
		BlockInput, off
	}
	; TrayIcon_Button("TIM.exe","L")
	; IfWinNotActive ahk_class SciTEWindow
	; {
		WinActivate,ahk_class TXGuiFoundation
		WinWaitActive,ahk_class TXGuiFoundation ,,5
		CoordWinClick(115, 29)
		click
		sleep,50
		Clipboard:=TemTIMName
		sleep,50
		; send,^{vk56}	;粘贴
		send,^{v}
		sleep,300
		send,{enter}		;确认搜索内容
		; ; msgbox %Clipboard%
		; Clipboard=
		; ; ;sendinput,% MyFun_uStrQ(TemTIMName)
		; ; ;~ sendinput,% MyFun_getAscStrQ(TemTIMName)
		; ; ; sleep,300
		; ; ; send,{enter}		;确认搜索内容
		; ; ; sleep,500
		SetTitleMatchMode,2
		if(TIMName!="搜索联系人")
		{
			WinActivate, %TemTIMName%		;通常不需要在 WinActivate 后使用 WinWaitActive 或 IfWinNotActive
			CoordWinClick(115, 29)
			click
		}
		sleep,200	
		loop,parse,MySel,`n,`r
		{
			WinClip.AppendFiles(A_LoopField)		;通过WinClip类将文件置入剪贴板
			sleep,100
		}
		send,^{vk56}	;粘贴文件
		;Clipboard=
		return
	; }
	; WinActivate, TXGuiFoundation
	; WinWaitActive, TXGuiFoundation ,,5
	; ControlClick, x115 y29, TIM,,,, Pos	;相对于窗体左上角的相对位置点击
	; WinGetPos,Winx,Winy,,, TIM
	; ControlClick, x%Winx% y%Winy%, TIM
	; msgbox, %Winx% %Winy%
	; sendinput,{Esc}{esc}{esc}{esc}{esc}		;跳出已有搜索

}
